package org.geektimes.injection.stream;


import org.geektimes.common.util.StringConvertUtil;

import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

/**
 * @FileName: StringConvertEnum
 * @Description: 参数转换器映射枚举
 * @Author: MaWenHao
 * @Created: 2021/3/1 - 下午10:39
 */
public enum StringConvertEnum {
    /**
     * 转为List<String>
     */
    STRING_TO_LIST_STRING("java.util.List<java.lang.String>", StringConvertUtil::stringToListString),
    STRING_TO_STRING("java.lang.String", StringConvertUtil::stringToString),
    ;

    /**
     * 参数类型
     */
    private String type;

    /**
     * 类型转换器
     */
    private StringConvertFunction<Object> converter;

    private static final StringConvertEnum[] values = StringConvertEnum.values();

    StringConvertEnum(String type, StringConvertFunction<Object> converter) {
        this.type = type;
        this.converter = converter;
    }

    public static StringConvertFunction<Object> getConverter(String value) {
        Optional<StringConvertEnum> first = Arrays.stream(values).filter(v -> Objects.equals(value, v.type)).findFirst();
        if (!first.isPresent()) {
            throw new RuntimeException("不支持的类型转换: " + value);
        }
        return first.get().converter;
    }
}
